import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Welcome from '../components/welcome.vue'
import Users from '../components/users.vue'
import Roles from '../components/permission/roles.vue'
import Right from '../components/permission/right.vue'
import comList from '../components/commodity/comList.vue'
import params from '../components/commodity/params.vue'
import List from '../components/commodity/list.vue'
import Add from '../components/commodity/add.vue'
import Order from '../components/order/orderList.vue'
import Statistic from '../components/statistic/statistic.vue'
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
const router = new VueRouter({
    routes: [
        { path: '/', redirect: '/login' },
        { path: '/login', component: Login },
        {
            path: '/home',
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
                { path: '/roles', component: Roles },
                { path: '/rights', component: Right },
                { path: '/categories', component: comList },
                { path: '/goods', component: List },
                { path: '/goods/add', component: Add },
                { path: '/params', component: params },
                { path: '/orders', component: Order },
                { path: '/reports', component: Statistic },
            ]
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next();
    const tokenstr = sessionStorage.getItem('token')
    if (!tokenstr) return next('/login');
    next();

})

export default router